package cn.yh.shop.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.yh.shop.user.dao.UserOrderDao;
import cn.yh.shop.user.dao.UserOrderItemDao;
import cn.yh.shop.user.model.UserOrder;
import cn.yh.shop.user.model.UserOrderItem;
import cn.yh.shop.user.service.IUserOrderService;
/**
 * @Description:订单的业务类
 * @author:"许荣斐"
 * @time:2016年12月23日 上午8:52:41
 */
@Service
public class UserOrderServiceImpl implements IUserOrderService {

	@Autowired
	private UserOrderDao userOrderDao;
	@Autowired
	private UserOrderItemDao userOrderItemDao;
	
	@Override
	public List<UserOrder> getUserOrders() {
		return userOrderDao.selectUserOrders();
	}

	/*
	 * 根据条件获取用户订单
	 */
	@Override
	public List<UserOrder> getUserOrderById(HashMap map) {
		return userOrderDao.selectUserOrderById(map);
	}

	@Override
	public List<UserOrder> getUserOrderByIdAndMonth(HashMap map) {
		return userOrderDao.selectUserOrderByIdAndMonth(map);
	}
	
	public int editUserOrder(UserOrder userorder) {
		return userOrderDao.editUserOrder(userorder);
	}
	
	public int deleteUserOrder(List<UserOrder> list){
		return userOrderDao.deleteUserorder(list);
	}

	@Override
	@Transactional
	public int createOrder(UserOrder userOrder) {
		int i = 0; 
		try {
			userOrderDao.createOrder(userOrder);
			List<UserOrderItem> items = userOrder.getOrderItems();
			for (UserOrderItem userOrderItem : items) {
				userOrderItemDao.createOrderItem(userOrderItem);
			}
			i = 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	/* 
	 * 根据条件动态查询用户订单
	 */
	@Override
	public List<UserOrder> dynQueryUserOrder(Map<String, Object> map) {
		List<UserOrder> order = userOrderDao.dynQueryUserOrder(map);
		
		for (UserOrder userOrder : order) {
			HashMap<String , String> ordermap=new HashMap<String , String>();
			ordermap.put("orderId", userOrder.getId());
			List<UserOrderItem> orderItems = userOrderItemDao.selectUserOrderItemById(ordermap);
			userOrder.setOrderItems(orderItems);
			ordermap.clear();
		}
		
		return order; 
	}


}
